package models;

import play.*;
import play.db.jpa.*;

import javax.persistence.*;

import java.util.*;

/** mohammed mattr */
 @Entity
/**mohammed mattr*/
 @Table(name = "LogRecord")
   public class LogRecord extends Model {
 @Lob
	/** start mohammed mattr*/
	private RegisteredUser user;
	private String action;
	private String date;

	/** end mohmmed mattr */

	public LogRecord(RegisteredUser user, String action, String date) {

		this.user = user;
		this.action = action;
		this.date = date;
		this.save();

	}

	/**
	 * This method creates the log of the actions of users
	 * 
	 * @author mohammed mattr * 
	 * @param action, u
	 *    The action user did and the user
	 *         
	 */

	public static void createLog(String action, RegisteredUser u) {

		Calendar now = Calendar.getInstance();
		List<LogRecord> logs = LogRecord.findAll();

		if (logs.size() == 10) {

			long smallest = 0;

			for (int i = 0; i < logs.size(); i++) {
				smallest = logs.get(i).id;

				for (int k = 0; k < logs.size(); k++) {
					if (logs.get(k).id < smallest) {
						smallest = logs.get(k).id;
					}
				}
			}
			LogRecord.findById(smallest)._delete();
		}

		LogRecord log = new LogRecord(u, action, "Action occured in: " +

		      +(now.get(Calendar.MONTH) + 1) 
		      + "-" 
		      + now.get(Calendar.DATE) 
		      + "-"
			  + now.get(Calendar.YEAR)
			  + "\n" + "at " + "\n"
			  + now.get(Calendar.HOUR_OF_DAY) + ":"
			  + now.get(Calendar.MINUTE) + ":" 
			  + now.get(Calendar.SECOND)
			  + "." + now.get(Calendar.MILLISECOND));

	}
	
	/**
	 * This is the getter method of user
	 * returns user
	 * @author mohammed mattr * 
	 */
	public RegisteredUser getUser() {
		return user;
	}

	/**
	 * This is the setter method of user
	 * @author mohammed mattr 
	 * @param  user
	 *             the user did the action
	 */
	public void setUser(RegisteredUser user) {
		this.user = user;
	}
	/**
	 * This is the getter method of Action
	 * returns action
	 * @author mohammed mattr * 
	 */

	public String getAction() {
		return action;
	}
	/**
	 * This is the setter method of action
	 * @author mohammed mattr 
	 * @param  action
	 *             the action done by the user
	 */

	public void setAction(String action) {
		this.action = action;
	}

	/**
	 * This is the getter method of date
	 * returns date
	 * @author mohammed mattr * 
	 */


	public String getDate() {
		return date;
	}
	/**
	 * This is the setter method of date
	 * @author mohammed mattr 
	 * @param  date
	 *             the date and time of the action
	 */

	public void setDate(String date) {
		this.date = date;
	}

}
